package com.na.tools.texturesplit;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.na.tools.texturesplit.TextureFrame.TableModel;

public class TextureSplitUI extends JFrame implements ActionListener, ListSelectionListener {

	private static final long serialVersionUID = -5136574086324994988L;
	protected static final String ACTION_IMPORT = ".import";
	protected static final String ACTION_EXPORT = ".export";
	
	private JLabel lblPath;
	private TexturePanel pnlTexture;
	private JTable tblFrames;
	
	public TextureSplitUI() {
		setTitle("Texture Split");
		setSize(800, 480);
		setLayout(new BorderLayout());
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		init();
		setLocationRelativeTo(null);
		setVisible(true);
	}
	
	protected void init() {
		JPanel top = new JPanel();
		add(top, BorderLayout.NORTH);
		top.add(lblPath = new JLabel());
		top.add(newButton("导入", ACTION_IMPORT, this));
		top.add(newButton("导出", ACTION_EXPORT, this));
		add(pnlTexture = new TexturePanel(), BorderLayout.CENTER);
		
		JTable table = new JTable(new TextureFrame.TableModel());
		JScrollPane scroll = new JScrollPane(table);
		add(scroll, BorderLayout.EAST);
		table.getSelectionModel().addListSelectionListener(this);
		tblFrames = table;
	}
	
	protected static JButton newButton(String text, String cmd, ActionListener listener) {
		JButton btn = new JButton(text);
		btn.setActionCommand(cmd);
		btn.addActionListener(listener);
		return btn;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if (ACTION_IMPORT.equals(cmd)) {
			showImportDialog();
		} else if (ACTION_EXPORT.equals(cmd)) {
			showExportDialog();
		}
	}

	private void showExportDialog() {
		JFileChooser chooser = new JFileChooser(TextureTools.getSetting("plist"));
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		chooser.setApproveButtonText("导出");
		chooser.showSaveDialog(null);
		File dir = chooser.getSelectedFile();
		if (dir != null) {
			TableModel tm = (TableModel) tblFrames.getModel();
			int succesCount = 0, failCount = 0;
			for (TextureFrame f : tm.getRowsData()) {
				try {
					f.exportTo(dir);
					succesCount++;
				} catch (IOException e) {
					e.printStackTrace();
					failCount++;
				}
			}
			try {
				Desktop.getDesktop().open(dir);
			} catch (IOException e) {
			}
			JOptionPane.showMessageDialog(null, "Export Result: success=" + succesCount + ", fail=" + failCount, "Export Result", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	private void showImportDialog() {
		JFileChooser chooser = new JFileChooser(TextureTools.getSetting("plist"));
		chooser.addChoosableFileFilter(new FileNameExtensionFilter("plist", "plist"));
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		chooser.setApproveButtonText("导入");
		chooser.showOpenDialog(null);
		File file = chooser.getSelectedFile();
		if (file != null) {
			TextureTools.saveSetting("plist", file.getAbsolutePath());
			try {
				Collection<TextureFrame> frames = TextureTools.parsePlist(file);
				pnlTexture.clearTextureFrames();
				pnlTexture.addTextureFrames(frames);
				TableModel tm = (TableModel) tblFrames.getModel();
				tm.clearRowsData();
				tm.addRows(frames);
			} catch (Exception e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "Parse file error", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
	}
	
}
